package com.yunshang.ysmall.service.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yunshang.ysmall.mapper.MbAddrInfoMapper;
import com.yunshang.ysmall.mapper.MemberInfoMapper;
import com.yunshang.ysmall.mapper.OrderInfoMapper;
import com.yunshang.ysmall.mapper.OrderItemInfoMapper;
import com.yunshang.ysmall.model.MbAddrInfo;
import com.yunshang.ysmall.model.MemberInfo;
import com.yunshang.ysmall.model.OrderInfo;
import com.yunshang.ysmall.model.OrderItemInfo;
import com.yunshang.ysmall.service.MemberService;
@Service
public class MemberServiceImpl implements MemberService {
	@Resource
	MemberInfoMapper memberInfoMapper;
	@Resource
	MbAddrInfoMapper mbAddrInfoMapper;
	@Resource
	OrderInfoMapper orderInfoMapper;
	@Resource
	OrderItemInfoMapper orderItemInfoMapper;

	@Override
	public void loginHandle(MemberInfo memberInfo) {
		// TODO Auto-generated method stub
		if(memberInfoMapper.selectByMemberId(memberInfo.getMemberId()) != null){
			memberInfoMapper.update(memberInfo);
		}else{
			memberInfoMapper.insert(memberInfo);
		}
	}

	@Override
	public MemberInfo getMemberById(String memberId) {
		// TODO Auto-generated method stub
		return memberInfoMapper.selectByMemberId(memberId);
	}

	@Override
	public boolean doPay(MemberInfo memberInfo) {
		// TODO Auto-generated method stub
		return memberInfoMapper.dopay(memberInfo) > 0 ? true : false;
	}

	@Override
	public boolean addMbAddr(MbAddrInfo mbAddrInfo) {
		// TODO Auto-generated method stub
		return mbAddrInfoMapper.insert(mbAddrInfo) > 0 ? true : false;
	}

	@Override
	public boolean delMbAddr(Integer addrId) {
		// TODO Auto-generated method stub
		return mbAddrInfoMapper.deleteByPrimaryKey(addrId) > 0 ? true : false;
	}

	@Override
	public MbAddrInfo getAddrByMemberId(String memberId) {
		// TODO Auto-generated method stub
		return mbAddrInfoMapper.selectByMemberId(memberId);
	}

	@Override
	public boolean saveOrder(OrderInfo orderInfo) {
		// TODO Auto-generated method stub
		return orderInfoMapper.insert(orderInfo) > 0 ? true : false;
	}

	@Override
	public boolean updateOrder(OrderInfo orderInfo) {
		// TODO Auto-generated method stub
		return orderInfoMapper.update(orderInfo) >0 ? true : false;
	}

	@Override
	public boolean saveOrderItem(OrderItemInfo orderItemInfo) {
		// TODO Auto-generated method stub
		return orderItemInfoMapper.insert(orderItemInfo) >0 ? true : false;
	}

	@Override
	public List<OrderInfo> getOrderListByParams(Map params) {
		// TODO Auto-generated method stub
		List<OrderInfo> orderList = orderInfoMapper.getOrderListByParams(params);
		for (OrderInfo orderInfo : orderList) {
			orderInfo.setItemList(orderItemInfoMapper.getItemListByOrderId(orderInfo.getOrderId()));
		}
		return orderList;
	}

	@Override
	public List<OrderItemInfo> getOrderItemListByOrderId(String orderId) {
		// TODO Auto-generated method stub
		return orderItemInfoMapper.getItemListByOrderId(orderId);
	}

	@Override
	public OrderInfo getOrderInfoById(String orderId) {
		// TODO Auto-generated method stub
		return orderInfoMapper.selectByOrderId(orderId);
	}

	@Override
	public MbAddrInfo getAddrById(Integer mbAddrId) {
		// TODO Auto-generated method stub
		return mbAddrInfoMapper.selectById(mbAddrId);
	}

	@Override
	public Integer getListCountByParams(Map params) {
		// TODO Auto-generated method stub
		return orderInfoMapper.getListCountByParams(params);
	}

}
